package com.bigShen.jdbc.basic;

import java.sql.*;

/**
 * @author : lei.shen
 * @date : 2018/11/22
 */
public class JdbcHelloWorld {

    public static void main(String[] args) {

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:HTBK", "TRD_CAMS", "xpar");

//            -- select
//            statement = connection.prepareStatement("SELECT * FROM TMARKET WHERE M_TYPE = ?");
//            statement.setString(1,  "XSHG");
//            resultSet = statement.executeQuery();
//            while (resultSet.next()) {
//                String name = resultSet.getString("M_NAME");
//                String type = resultSet.getString("M_TYPE");
//                System.out.println(name + " " + type);
//            }

//            insert transation
            connection.setAutoCommit(false);
            statement = connection.prepareStatement("INSERT INTO TMARKET (M_TYPE, COUNTRY, M_NAME, Calendar) VALUES (?, ?, ?, ?)");
            statement.setString(1, "X_SHENs");
            statement.setString(2, "CN");
            statement.setString(3, "test");
            statement.setString(4, "CHINA_IB");
            resultSet = statement.executeQuery();
            connection.commit();

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != resultSet) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (null != statement) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (null != connection) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
